package org.testfire.realm.control.testfire;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.testfire.model.TestType;
import org.testfire.model.TestsetType;
import org.testfire.realm.control.exception.CouldNotCreateTestSetException;
import org.testfire.realm.model.entities.User;
import org.testfire.realm.model.entities.testfire.TestEntity;
import org.testfire.realm.model.entities.testfire.TestsetEntity;
import org.testfire.util.DomReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Aug 22, 2007
 * Time: 11:25:52 AM
 */
public class TestFireSubmitHandler implements Runnable
{
    private final User user;

    private final CharSequence value;

    Thread t;

    private TestsetEntity testsetEntity;

    private Exception exception;

    public TestFireSubmitHandler( User user, CharSequence value )
    {
        this.user = user;
        this.value = value;
    }

    public void start()
    {
        t = new Thread( this );
        t.start();
    }

    public boolean isAlive()
    {
        return t.isAlive();
    }

    public void run()
    {
        Logger log = Logger.getLogger( this.getClass() );
        try
        {
            // parse the document into a testset.
            Document doc = DomReader.getDocument( value );
            Element de = doc.getDocumentElement();
            TestsetType testset = new TestsetType( de );

            // get the new test testset entity and a DBAO to persit it with.
            TestFireDBAO tfd = TestFireDBAO.lookup();
            testsetEntity = new TestsetEntity();

            // testset the username field to that of the user that submitted it.
            // @todo this should probably be a running field of all users who have edited the test, and when.
            testsetEntity.setUsername( user.getName() );
            populate( testset, testsetEntity );
            tfd.persist( testsetEntity );
            log.info( "Persisted testset: " + testsetEntity.getName() );
        }
        catch ( Exception e )
        {
            this.exception = new CouldNotCreateTestSetException( e );
        }


    }

    private void populate( TestsetType testset, TestsetEntity entity )
    {
        entity.setCreated( new Timestamp( testset.getCreated().getTime() ) );
        entity.setLastverified( new Timestamp( testset.getCreated().getTime() ) );
        entity.setName( testset.getName() );
        entity.setDescription( testset.getDescription() );
        entity.setForbug( testset.getForBug() );
        List<TestEntity> testEntities = new ArrayList<TestEntity>();

        List<TestType> tests = testset.getTests();
        for ( TestType test : tests )
        {
            TestEntity testEntity = new TestEntity();
            populate( testset, test, testEntity );
            testEntities.add( testEntity );
        }
        entity.setTests( testEntities );
        entity.setTestsetdata( (String)testset.translate( testset ) );
    }

    private void populate( TestsetType testset, TestType test, TestEntity entity )
    {
        entity.setName( test.getName() );
        entity.setForbug( test.getForBug() );
        entity.setCreated( new Timestamp( test.getCreated().getTime() ) );
        entity.setDescription( test.getDescription() );
        entity.setTestdata( (String)test.translate( testset ) );
    }

    public Exception getException()
    {
        return exception;
    }

    public TestsetEntity getTestsetEntity()
    {
        return testsetEntity;
    }
}




